﻿using FineUI;
using System;
using System.Linq;
using System.Text;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PublicCity
{
    public partial class PartyPublicCityEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyPublicCityAdd";
            }
        }

        public string PublicCityModule
        {
            get
            {
                return this.Request["module"] ?? "";
            }
        }

        public string Category
        {
            get { return this.Request["Category"] ?? ""; }
        }
        #endregion


        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                partyjoinpublicity PublicCity = DB.partyjoinpublicitys.Find(id);
                if (PublicCity == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtTitle.Text = PublicCity.Title;
                //txtLoginID.Text = PublicCity.LoginID;

                IQueryable<partyjoinpublicity> q = DB.partyjoinpublicitys.Where(t => t.ID == id);

                if (q.Count() > 0)
                {
                    StringBuilder sbLoginID = new StringBuilder();
                    StringBuilder sbName = new StringBuilder();

                    foreach (partyjoinpublicity item in q)
                    {
                        sbLoginID.Append(item.LoginID + ",");
                        sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                    }

                    ppMember.Value = sbLoginID.ToString().TrimEnd(',');
                    ppMember.Text = sbName.ToString().TrimEnd(',');
                }

                txtStepID.SelectedValue = PublicCity.StepID;
                txtCreateTime.Text = Convert.ToDateTime(PublicCity.CreateTime).ToString("yyyy-MM-dd HH:mm");
                txtNoticeTime.Text = PublicCity.NoticeTime;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");
            string _id = id;
            string imageUrl = string.Empty;
            partyjoinpublicity PublicCity;

            if (id != "")
            {
                //更新
                PublicCity = DB.partyjoinpublicitys.Find(id);
                if (PublicCity == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                PublicCity.Title = txtTitle.Text;
                PublicCity.StepID = txtStepID.SelectedValue;
                PublicCity.NoticeTime = txtNoticeTime.Text;

                if (!string.IsNullOrEmpty(ppMember.Value))
                {
                    foreach (string item in ppMember.Value.Split(','))
                    {
                        PublicCity.LoginID = item;
                    }
                }
                DB.SaveChanges();
            }
            else
            {
                //新增
                PublicCity = new partyjoinpublicity();
                PublicCity.ID = StringHelper.NewID;
                PublicCity.CreateTime = DateTime.Now;
                PublicCity.Title = txtTitle.Text;
                PublicCity.StandID = null;
                PublicCity.AppllicationID = null;
                PublicCity.StepID= txtStepID.SelectedValue;
                PublicCity.NoticeTime = txtNoticeTime.Text;
                PublicCity.Category = "publicity";
                if (!string.IsNullOrEmpty(ppMember.Value))
                {
                    foreach (string item in ppMember.Value.Split(','))
                    {
                        PublicCity.LoginID = item;

                        DB.partyjoinpublicitys.Add(PublicCity);

                        DB.SaveChanges();
                    }
                }

            } 
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        #endregion
    }
}